package zk.tool.util;

import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

public class AlertUtil {
    private static final Logger log = LoggerFactory.getLogger(AlertUtil.class);

    public static void info(String title, String msg) {
        newAlert(Alert.AlertType.INFORMATION, title, msg).showAndWait();
    }

    public static void warning(String title, String msg) {
        newAlert(Alert.AlertType.WARNING, title, msg).showAndWait();
    }

    public static void error(String title, String error) {
        newAlert(Alert.AlertType.ERROR, title, error).showAndWait();
    }

    public static void exception(String title, Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String errMsg = sw.toString();
        try {
            sw.close();
            pw.close();
        } catch (IOException ex) {
            log.error("close writer exception: ", ex);
        }

        Stage stage = newDialog(null, 600, 400);
        stage.setResizable(true);

        Label label = new Label(title);
        TextArea textArea = new TextArea(errMsg);
        textArea.setEditable(false);
        textArea.setWrapText(true);

        textArea.setMaxWidth(Double.MAX_VALUE);
        textArea.setMaxHeight(Double.MAX_VALUE);
        GridPane.setVgrow(textArea, Priority.ALWAYS);
        GridPane.setHgrow(textArea, Priority.ALWAYS);

        GridPane expContent = new GridPane();
        expContent.setMaxWidth(Double.MAX_VALUE);
        expContent.add(label, 0, 0);
        expContent.add(textArea, 0, 1);

        Scene scene = new Scene(expContent, Double.MAX_VALUE, Double.MAX_VALUE);
        stage.setScene(scene);
        stage.showAndWait();
    }

    /**
     * 新建一个dialog窗口，但是没有Scene，需自己设置Scene
     *
     * @param parent 父窗口，如果没有就是独立的窗口
     * @param width  宽度，默认600
     * @param height 高度，默认400
     * @return @Stage
     */
    public static Stage newDialog(Stage parent, double width, double height) {
        Stage stage = new Stage();
        if (parent != null) {
            stage.initOwner(parent);
        }
        if (width <= 0.0D) {
            width = 600.0D;
        }
        if (height <= 0.0D) {
            height = 400.00D;
        }
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.setResizable(false);
        stage.setFullScreen(false);
        stage.centerOnScreen();
        stage.setHeight(height);
        stage.setWidth(width);
        return stage;
    }

    private static Alert newAlert(Alert.AlertType type, String header, String msg) {
        Alert alert = new Alert(type);
        alert.setHeaderText(header);
        alert.setContentText(msg);
        return alert;
    }
}
